// FiveView.cpp : implementation of the CFiveView class
//

#include "stdafx.h"
#include "Five.h"

#include "FiveDoc.h"
#include "FiveView.h"
#include "simpleAi.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFiveView

#define FLASH_CHESS 0x1234

IMPLEMENT_DYNCREATE(CFiveView, CFormView)

BEGIN_MESSAGE_MAP(CFiveView, CFormView)
	//{{AFX_MSG_MAP(CFiveView)
	ON_WM_PAINT()
	ON_COMMAND(ID_TWO_HUMAN, OnTwoHuman)
	ON_COMMAND(ID_HUMAN_FIRST, OnHumanFirst)
	ON_UPDATE_COMMAND_UI(ID_HUMAN_FIRST, OnUpdateHumanFirst)
	ON_COMMAND(ID_COMPUTER_FIRST, OnComputerFirst)
	ON_UPDATE_COMMAND_UI(ID_COMPUTER_FIRST, OnUpdateComputerFirst)
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_RBUTTONDOWN()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
	ON_MESSAGE(MSG_PUT_CHESS,OnPutChess)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFiveView construction/destruction

CFiveView::CFiveView()
	: CFormView(CFiveView::IDD)
{
	//{{AFX_DATA_INIT(CFiveView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CFiveView::~CFiveView()
{
}

void CFiveView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFiveView)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BOOL CFiveView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CFiveView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
	
	this->SetTimer(FLASH_CHESS,200,0);
}

/////////////////////////////////////////////////////////////////////////////
// CFiveView diagnostics

#ifdef _DEBUG
void CFiveView::AssertValid() const
{
	CFormView::AssertValid();
}

void CFiveView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CFiveDoc* CFiveView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFiveDoc)));
	return (CFiveDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CFiveView message handlers

void CFiveView::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	this->GetDocument()->m_cb.DrawAll(&dc);
	// Do not call CFormView::OnPaint() for painting messages
}

void CFiveView::OnTwoHuman() 
{
	// TODO: Add your command handler code here
	if(RestartGame())
	{
		CFiveDoc* pDoc = GetDocument();
		pDoc->StartGame();
		this->InvalidateRect(NULL);
	}
}

void CFiveView::OnHumanFirst() 
{
	// TODO: Add your command handler code here
	if(RestartGame())
	{
		CFiveDoc* pDoc = GetDocument();
		pDoc->StartGame(HUMAN_VS_COMPUTER,true);
		this->InvalidateRect(NULL);
	}
}

void CFiveView::OnUpdateHumanFirst(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	CFiveDoc* pDoc = GetDocument();
	if(pDoc->IsStart() && pDoc->GetGameType()==HUMAN_VS_COMPUTER && pDoc->IsHumanFirst())
	{
		pCmdUI->SetCheck();
	}
	else
	{
		pCmdUI->SetCheck(0);
	}
}

void CFiveView::OnComputerFirst() 
{
	// TODO: Add your command handler code here
	if(RestartGame())
	{
		CFiveDoc* pDoc = GetDocument();
		pDoc->StartGame(HUMAN_VS_COMPUTER,false);
		this->InvalidateRect(NULL);
	}
}

void CFiveView::OnUpdateComputerFirst(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	CFiveDoc* pDoc = GetDocument();
	if(pDoc->IsStart() && pDoc->GetGameType()==HUMAN_VS_COMPUTER && !pDoc->IsHumanFirst())
	{
		pCmdUI->SetCheck();
	}
	else
	{
		pCmdUI->SetCheck(0);
	}	
}

void CFiveView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CFiveDoc* pDoc = GetDocument();
	if(pDoc->IsStart())
	{
		CChessBoard &cb = pDoc->m_cb;
		int x, y;
		if(cb.GetPos(point,x,y))
		{
			this->SendMessage(MSG_PUT_CHESS,(WPARAM)x,(LPARAM)y);
		}
	}
	CFormView::OnLButtonDown(nFlags, point);
}

void CFiveView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CFiveDoc* pDoc = GetDocument();
	if(pDoc->IsStart())
	{
		CChessBoard &cb = pDoc->m_cb;
		cb.DrawFocusRect(this->GetDC(),point);
	}
	CFormView::OnMouseMove(nFlags, point);
}

LRESULT CFiveView::OnPutChess(WPARAM wParam, LPARAM lParam)
{
	int x = wParam, y = lParam;
	CFiveDoc* pDoc = GetDocument();
	int result = pDoc->PutChess(x,y);

	CChessBoard &cb = pDoc->m_cb;

	if(result!=-1)
	{
		cb.DrawChess(this->GetDC(),x,y);
	}
	if(result==1)//GAME OVER
	{
		if(cb.GetCurrentPlayer()==BLACK_CHESS)
		{
			this->MessageBox("Black Side Win!!!");
		}
		else
		{
			this->MessageBox("White Side Win!!!");
		}
		pDoc->StopGame();
	}
	return NULL;
}

BOOL CFiveView::RestartGame()
{
	CFiveDoc* pDoc = GetDocument();
	if(pDoc->IsStart())
	{
		if(MessageBox("Are you sure to restart the game?","Confirm",MB_ICONQUESTION|MB_OKCANCEL) != IDOK)
		{
			return FALSE;
		}
		pDoc->StopGame();
	}
	return TRUE;
}

void CFiveView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CFiveDoc* pDoc = GetDocument();
	if(pDoc->IsStart())
	{
		CChessBoard &cb = pDoc->m_cb;
		int x, y;
		if(cb.GetPos(point,x,y))
		{
			if(pDoc->cp)
			{
				//pDoc->cp->m_pAI->UpdateStatus(x,y);
				this->MessageBox(pDoc->cp->m_pAI->GetStatus(x,y).FormatStatusString().c_str());
			}
		}
	}
	CFormView::OnRButtonDown(nFlags, point);
}

void CFiveView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	CFiveDoc* pDoc = GetDocument();
	switch(nIDEvent)
	{
	case FLASH_CHESS:
		{
			if(pDoc->IsStart())
			{
				pDoc->m_cb.FlashChess(this->GetDC());
			}
		}
		break;
	default:
		break;
	}
	CFormView::OnTimer(nIDEvent);
}

